'use strict'

import { BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
const path = require('path')
const windowOpacity = require('./windowOpacity.js')
let mainWindow = null // 主窗口

// 创建窗口
async function createWindow() {
  mainWindow = new BrowserWindow({
    title: '主窗口',
    width: 1240,
    height: 730,
    show: true,
    center: true,
    frame: false, // 去掉窗口边框
    autoHideMenuBar: true, // 隐藏菜单栏
    titleBarStyle: 'customButtonsOnHover',
    simpleFullscreen: true,
    resizable: process.platform === 'darwin', // 可否调整大小
    movable: true, // 可否移动
    minimizable: true, // 可否最小化
    maximizable: true, // 可否最大化
    fullscreen: false, // MAC下是否可以全屏
    skipTaskbar: false, // 在任务栏中显示窗口
    acceptFirstMouse: true, // 是否允许单击页面来激活窗口
    transparent: process.platform === 'darwin', //允许透明
    closable: true,
    backgroundColor: '#fff',
    allowRunningInsecureContent: true, // 允许一个 https 页面运行 http url 里的资源
    webPreferences: {
      devTools: true, // 是否打开调试模式
      webSecurity: false, // 禁用安全策略
      allowDisplayingInsecureContent: true, // 允许一个使用 https的界面来展示由 http URLs 传过来的资源
      allowRunningInsecureContent: true, // 允许一个 https 页面运行 http url 里的资源
      webviewTag: true,
      plugins: true,
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      // nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION
      enableRemoteModule: true,
      nodeIntegration: true,
      // preload: process.env.WEBPACK_DEV_SERVER_URL ? path.join(__dirname, '../public/preload.js') : `app://./preload.js`
      preload: process.env.WEBPACK_DEV_SERVER_URL ? path.join(__dirname, '../public/preload.js') : path.join(__dirname, 'preload.js')
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    // mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL, {
    //   userAgent: 'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255'
    // })
    await mainWindow.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    // if (!process.env.IS_TEST) mainWindow.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    // mainWindow.loadURL('app://./index.html', {
    //   userAgent: 'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255'
    // })
    await mainWindow.loadURL('app://./index.html')
  }

  mainWindow
  .on('ready-to-show', () => { // 完成第一次绘制后显示
    // 程序界面淡入
    windowOpacity(mainWindow)
  })
  .on('close', (event) => { // 监听关闭
    console.log('mainWindow close:', event)
    // mainWindow.send('close-window-render')
    // event.preventDefault()
  })
  .on('closed', () => {
    mainWindow = null
  })
}

// 窗口 - 关闭
function close () {
  if (mainWindow && !mainWindow.isDestroyed()) {
    mainWindow.destroy()
  }
}

// 暴露API
export default {
  init (callback) {
    close()
    createWindow()
    if (callback instanceof Function) callback(mainWindow)
  },
  getId () {
    let result = null
    if (mainWindow && mainWindow.webContents) {
      result = mainWindow.webContents.id
    }
    return result
  },
  getWindow () {
    return mainWindow
  },
  close
}
